package cn.dgut.training.dto;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class UserDTO extends BaseDTO{

    @ApiModelProperty(value = "用户名称")
    private String username;

    @ApiModelProperty(value = "用户昵称或网络名称")
    private String nickname;

    @ApiModelProperty(value = "用户密码")
    private String password;

    @ApiModelProperty(value = "性别：0 未知， 1男， 1 女")
    private Integer gender;

    @ApiModelProperty(value = "生日")
    private LocalDate birthday;

    @ApiModelProperty(value = "最近一次登录时间")
    private LocalDateTime lastLoginTime;

    @ApiModelProperty(value = "最近一次登录IP地址")
    private String lastLoginIp;

    @ApiModelProperty(value = "0 管理员，1 普通用户")
    private Integer userLevel;

    @ApiModelProperty(value = "用户手机号码")
    private String mobile;

    @ApiModelProperty(value = "用户头像图片")
    private String avatar;

    @ApiModelProperty(value = "微信登录openid")
    private String weixinOpenid;

    @ApiModelProperty(value = "微信登录会话KEY")
    private String sessionKey;

    @ApiModelProperty(value = "0 可用, 1 禁用, 2 注销")
    private Integer status;

    public static QueryWrapper getWrapper(UserDTO userDTO){

        QueryWrapper queryWrapper=new QueryWrapper();

        if(!StringUtils.isBlank(userDTO.getUsername())){
            queryWrapper.like("username",userDTO.getUsername());
        }
        if(!StringUtils.isBlank(userDTO.getMobile())){
            queryWrapper.like("mobile",userDTO.getMobile());
        }
        if(!StringUtils.isBlank(userDTO.getWeixinOpenid())){
            queryWrapper.like("weixin_openid",userDTO.getWeixinOpenid());
        }
        if(!StringUtils.isBlank(userDTO.getOrderBy())){
            if("desc".equals(userDTO.getSort())){
                queryWrapper.orderByDesc(userDTO.getOrderBy());
            }else {
                queryWrapper.orderByAsc(userDTO.getOrderBy());
            }

        }
        return queryWrapper;
    }
}
